package com.todo.content.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.content.application.backend.service.ContentInfoBackendService;
import com.todo.content.application.convert.ContentConvert;
import com.todo.content.application.entity.bo.ContentDetailBO;
import com.todo.content.application.entity.bo.ContentInfoBO;
import com.todo.content.application.entity.condition.ContentInfoConditionBO;
import com.todo.content.application.service.ContentInfoService;
import com.todo.content.common.enums.ContentStatusEnum;
import com.todo.content.iface.backend.request.*;
import com.todo.content.iface.backend.response.ContentBaseInfoResp;
import com.todo.content.iface.backend.response.ContentDetailResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (ContentInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:45
 */
@Slf4j
@Service("contentInfoBackendService")
public class ContentInfoBackendServiceImpl implements ContentInfoBackendService {

    @Resource
    private ContentInfoService contentInfoService;

    @Override
    public BizResponse<Boolean> removeContentById(RemoveContentByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        Boolean removeResult = contentInfoService.removeContentDetailById(req.getContentId());
        log.info("ContentInfoBackendService#removeContentById 删除内容完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), removeResult);
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<ContentDetailResp> queryContentDetailById(QueryContentDetailByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentDetailBO contentDetailBO = contentInfoService.queryContentDetailById(req.getContentId());
        log.info("ContentInfoBackendService#queryContentDetailById 查询内容详情完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), JSONObject.toJSONString(contentDetailBO));
        return BizResponse.success(ContentConvert.convertToContentDetailResp(contentDetailBO));
    }

    @Override
    public BizResponse<Boolean> disableContentById(DisableContentByIdReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentDetailBO contentDetailBO = contentInfoService.queryContentDetailById(req.getContentId());
        if (contentDetailBO == null) {
            log.warn("ContentInfoBackendService#disableContentById 内容不存在 入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (ContentStatusEnum.DISCARD.equals(contentDetailBO.getContentInfoBO().getStatus())) {
            log.warn("ContentInfoBackendService#disableContentById 已废弃数据无法操作 入参：【{}】", JSONObject.toJSONString(req));
            throw new BizException(BizErrorCodeEnum.CONTENT_STATUS_OPERATE_PROHIBITED);
        }
        ContentInfoBO contentInfoBO = contentDetailBO.getContentInfoBO();
        if (ContentStatusEnum.DISABLE.equals(contentInfoBO.getStatus()) && !req.getEnable()) {
            log.info("ContentInfoBackendService#disableContentById 操作成功，当前状态已经为关闭状态  入参：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success(true);
        } else if (ContentStatusEnum.PUBLISHED.equals(contentInfoBO.getStatus()) && req.getEnable()) {
            log.info("ContentInfoBackendService#disableContentById 操作成功，当前状态已经为发布状态  入参：【{}】", JSONObject.toJSONString(req));
            return BizResponse.success(true);
        }
        ContentStatusEnum statusEnum = req.getEnable() ? ContentStatusEnum.PUBLISHED : ContentStatusEnum.DISABLE;
        Boolean disableResult = contentInfoService.updateContentStatus(req.getContentId(), contentInfoBO.getStatus(), statusEnum);
        log.info("ContentInfoBackendService#disableContentById 关闭内容完成 入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), disableResult);
        return BizResponse.success(disableResult);
    }

    @Override
    public BizPageResponse<ContentBaseInfoResp> queryContentInfoByCondition(QueryContentInfoByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentInfoConditionBO conditionBO = ContentConvert.convertToContentInfoConditionBO(req);
        PageResult<ContentInfoBO> pageResult = contentInfoService.queryContentInfoByCondition(conditionBO);
        return BizPageResponse.success(ContentConvert.convertToPageContentBaseInfoResp(pageResult));
    }

    @Override
    public BizResponse<Boolean> detectionContentById(DetectionContentReq req) {
        if (req == null || !req.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentDetailBO contentDetailBO = contentInfoService.queryContentDetailById(req.getContentId());
        if (contentDetailBO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!ContentStatusEnum.AUTO_EXAMINE_FAILED.equals(contentDetailBO.getContentInfoBO().getStatus())) {
            log.info("ContentInfoBackendService#detectionContentById 审核内容是否合法，当前状态不许人工介入或已执行完成 默认成功 contentDetailBO：{}", JSONObject.toJSONString(contentDetailBO));
            return BizResponse.success(true);
        }
        Boolean checkResult;
        //默认成功则发布，不成功则按照人工失败处理
        if (req.getPass()) {
            checkResult = contentInfoService.updateContentStatus(req.getContentId(), ContentStatusEnum.AUTO_EXAMINE_FAILED, ContentStatusEnum.PUBLISHED);
        } else {
            checkResult = contentInfoService.updateContentStatus(req.getContentId(), ContentStatusEnum.AUTO_EXAMINE_FAILED, ContentStatusEnum.EXAMINE_FAILED);
        }
        log.info("ContentInfoBackendService#detectionContentById 审核内容是否合法 入参：【{}】，出参：【{}】", JSONObject.toJSONString(req), checkResult);
        return BizResponse.success(checkResult);
    }
}
